﻿using System;
using System.Configuration;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;


namespace mojoPortal.Web.RS.CRM
{
    public partial class RSActivityHistory : RSBasePage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitializeReadyServicePage("Activity History");
                RSUserIdentity userIdentity = GetRSUserIdentity();
                gvActivityHistory.DataSource = GetActivityHistory(userIdentity.RSCompanyId, userIdentity.RSEmployeeId);
                gvActivityHistory.DataBind();
            }
        }

        private DataSet GetActivityHistory(int companyid, int employeeid)
        {
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;

            String sqlSELECT = "select h.activityhistoryid, c.activitycode, h.accountid, h.history, h.audit_createdt, e.firstname + ' ' + e.lastname as agentname from rs_tblActivityHistory h inner join rs_tblActivityCode c on h.ActivityCodeID=c.ActivityCodeID inner join rs_tblclient cl on h.accountid=cl.accountid left join rs_tblemployee e on e.employeeid=cl.agentid where h.CompanyID=@companyid and h.accountid in(" + GetAllAccountIDFromMyDownline(employeeid, companyid) + ") order by h.audit_createdt desc";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);

            return ds;
        }
        public string GetAllAccountIDFromMyDownline(int employeeid, int companyid)
        {
            string accountids = "";
            SqlCommand myCommand;
            SqlDataReader myDataReader;
            SqlParameter param;
            SqlConnection myConnection;
            
            String sqlSELECT = "exec rs_uspGetAllAccountIDFromMyDownline @EmployeeId, @CompanyId";
            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@employeeid";
            param.Value = employeeid;
            myCommand.Parameters.Add(param);

            myConnection.Open();

            myDataReader = myCommand.ExecuteReader(CommandBehavior.Default);
            if (myDataReader.HasRows)
            {
                while (myDataReader.Read())
                {
                    if (accountids.Length > 0)
                        accountids = accountids + ",";
                    accountids = accountids + "'" + myDataReader["accountid"].ToString() + "'";
                }
            }
            myConnection.Close();
            
            return accountids;
        }
    }

}

